/*
 * name: mabel xu
 * date: september 24, 2007
 * filename: MatrixMultiplication.java
 * description: reads matrices from a (preformatted) file, multiples them, then outputs the result
 */

import java.util.*;
import java.io.*;

public class MatrixMultiplication
{
	public static int[][] readMatrix(int rows, int cols, Scanner inFile)
	{
		int[][] matrix = new int[rows][cols];

		for(int i = 0; i < rows; i++)
			for(int j = 0; j < cols; j++)
				matrix[i][j] = inFile.nextInt();

		return matrix;
	}

	public static int[][] calculateMultiplication(int[][] one, int[][] two)
	{
		if (one[0].length != two.length)
		{
			return null;
		}
		else
		{
			int[][] ans = new int[one.length][two[0].length];
			int sum = 0;
			for(int i = 0; i < ans.length; i++)
				for(int j = 0; j < ans[0].length; j++)
				{
					for(int k = 0; k < one[0].length; k++)
						sum += one[i][k] * two[k][j];
					ans[i][j] = sum;
					sum = 0;
				}
			return ans;
		}
	}

	public static void printProblem(int[][] one, int[][] two, int[][] ans)
	{
		System.out.println("Matrix 1: ");
		for(int i = 0; i < one.length; i++)
		{
			System.out.print(i == 0 ? "[[\t" : " [\t");
			for(int j = 0; j < one[0].length; j++)
				System.out.print(one[i][j] + "\t");
			System.out.print(i == one.length - 1 ? "]]\n" : "]\n");
		}
		System.out.println();

		System.out.println("Matrix 2: ");
		for(int i = 0; i < two.length; i++)
		{
			System.out.print(i == 0 ? "[[\t" : " [\t");
			for(int j = 0; j < two[0].length; j++)
				System.out.print(two[i][j] + "\t");
			System.out.print(i == one.length - 1 ? "]]\n" : "]\n");
		}
		System.out.println();

		if (ans == null)
		{
			System.out.println("Matrices are of incompatible types");
		}
		else
		{
			System.out.println("Resulting Matrix: ");
			for(int i = 0; i < ans.length; i++)
			{
				System.out.print(i == 0 ? "[[\t" : " [\t");
				for(int j = 0; j < ans[0].length; j++)
					System.out.print(ans[i][j] + "\t");
				System.out.print(i == one.length - 1 ? "]]\n" : "]\n");
			}
		}

		System.out.println();
	}

	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);

		System.out.println("Welcome to Mabel's Matrix Multiplication Program");
		System.out.println();
		System.out.print("matrix input file? ");
		String fileName = input.next();
		System.out.println();
		try
		{
			Scanner inFile = new Scanner(new File(fileName));

			int probNumber = 1;
			int rows, cols;
			int[][] matrix1, matrix2, answer;

			try
			{
				while(inFile.hasNext())
				{
					rows = inFile.nextInt();
					inFile.next();	// skips the "by"
					cols = inFile.nextInt();
					matrix1 = readMatrix(rows, cols, inFile);
					rows = inFile.nextInt();
					inFile.next();	// skips the "by"
					cols = inFile.nextInt();
					matrix2 = readMatrix(rows, cols, inFile);
					answer = calculateMultiplication(matrix1, matrix2);

					System.out.println("Problem " + probNumber);
					printProblem(matrix1, matrix2, answer);

					probNumber++;
				}
			}
			catch(Exception e)
			{
				System.out.println("sorry, something's wrong with the file");
			}
		}
		catch(Exception e)
		{
			System.out.println("sorry, input file not found");
		}
	}
}